package utils

import (
	"crypto/md5"
	"encoding/hex"
	"golang.org/x/crypto/bcrypt"
)

const (
	secret = "Today"
)

// EncryptPassword 密码加密
func EncryptPassword(passwd string) string {
	hash, _ := bcrypt.GenerateFromPassword([]byte(passwd), bcrypt.DefaultCost)
	return string(hash)
}

// EncryptMd5  md5 加密字符串
func EncryptMd5(s string) string {
	h := md5.New()
	h.Write([]byte(secret))
	return hex.EncodeToString(h.Sum([]byte(s)))
}

// CheckPassword  校验密码，passwd 明文密码，encpwd 加密密码
func CheckPassword(passwd, encpwd string) bool {
	return bcrypt.CompareHashAndPassword([]byte(encpwd), []byte(passwd)) == nil
}
